package com.hotchpotch.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hotchpotch.R;
import com.hotchpotch.bean.DBFlowTestBean;
import com.hotchpotch.inter.OnRecyclerViewItemClickListener;

import java.util.List;

public class DBFlowAdapter extends RecyclerView.Adapter implements View.OnClickListener {
    private Context context;
    private List<DBFlowTestBean> list;
    private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public DBFlowAdapter(Context context) {
        this.context = context;
    }

    public void setList(List<DBFlowTestBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_dbflow, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ((ViewHolder) holder).tv1.setText("id = " + list.get(position).getId());
            ((ViewHolder) holder).tv2.setText("姓名 = " + list.get(position).getName());
            ((ViewHolder) holder).tv3.setText("性别 = " + list.get(position).getSex());
            ((ViewHolder) holder).tv4.setText("年龄 = " + list.get(position).getAge());
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public void onClick(View v) {
        if (onRecyclerViewItemClickListener != null) {
            onRecyclerViewItemClickListener.onItemClick(v);
        }
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv1, tv2, tv3, tv4;

        public ViewHolder(View itemView) {
            super(itemView);
            tv1 = (TextView) itemView.findViewById(R.id.item_dbflow_tv1);
            tv2 = (TextView) itemView.findViewById(R.id.item_dbflow_tv2);
            tv3 = (TextView) itemView.findViewById(R.id.item_dbflow_tv3);
            tv4 = (TextView) itemView.findViewById(R.id.item_dbflow_tv4);
        }
    }
}
